﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SCRUM.Core.BusinessObjects;
using SCRUM.Core.DAO;

public abstract partial class ReleaseDetails : HierarchicalObjectDetails<Release,Project>
{
    /// <summary>
    /// Method to be called on load
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
    protected void Page_Load(object sender, EventArgs e)
    {
        this.LoadExtendedInputsFrom(PanelItemDetails);
    }

    protected override IList<ScrumInput> LoadExtendedInputs()
    {
        return this.LoadExtendedInputsFrom(PanelItemDetails);
    }

    /// <summary>
    /// Loads the available superior items(Projects).
    /// </summary>
    protected override void LoadAvailableSuperiorItems()
    {
        AvailableSuperiorItems = DAOProject.GetAllProjects();
    }

    /// <summary>
    /// Displays all fields of the selected item into textboxes in form.
    /// </summary>
    public override void DisplayItem(Release release)
    {
        this.Visible = true;

        AllProjects.DataSource = AvailableSuperiorItems;
        AllProjects.DataBind();

        bool exists = release != null;

        // values
        this.Header.LastChange =    exists ? release.LastChange : DateTime.MinValue;
        this.Name.Text =            exists ? release.Name       : "";
        this.Version.Text =         exists ? release.Version    : "";
        this.Progress.Progress =    exists ? release.Progress   : 0;
        this.Estimation.Value =     exists ? release.Estimation : 0;
        int superiorItemID =        exists ? release.SuperiorItemID : this.SuperiorItemID;
        if (superiorItemID!=-1)
        {
            this.AllProjects.SelectedValue = superiorItemID.ToString();
        }

        // enabled
        this.AllProjects.Enabled = exists;

        // visibility
        this.Progress.Visible = exists;
        //this.Estimation.Visible = exists;
    }

    /// <summary>
    /// Creates new release
    /// </summary>
    /// <returns>Created release</returns>
    public override Release CreateItem()
    {
        int projectID = Int32.Parse(AllProjects.SelectedValue);
        Release rel = new Release(
            0,
            Name.Text,
            Version.Text,
            System.Convert.ToInt32(Estimation.Value),
            Progress.Progress,
            projectID,
            DateTime.Now
        );
        Release newRel = DAORelease.CreateNewRelease(rel, projectID);
        return newRel;
    }

    /// <summary>
    /// Edits release according to data in release detail panel
    /// </summary>
    /// <param name="relOld">Release to update</param>
    public override Release EditItem(int relOldId)
    {
        int projectID = Int32.Parse(AllProjects.SelectedValue);
        Release rel = new Release(
            relOldId, 
            Name.Text,
            Version.Text,
            System.Convert.ToInt32(Estimation.Value),
            Progress.Progress,
            projectID,
            DateTime.Now
        );
        DAORelease.UpdateRelease(rel, projectID);
        this.Visible = false;
        this.SuperiorItemID = projectID;
        DisplayItem(rel);
        return rel;
    }

    /// <summary>
    /// Deletes release
    /// </summary>
    /// <param name="rel">Release to delete</param>
    public override void DeleteItem(Release rel)
    {
        DAORelease.DeleteRelease(rel);
        this.Visible = false;
    }

    /// <summary>
    /// Validates the input.
    /// </summary>
    /// <returns>If the input is valid</returns>
    public override bool ValidateInput()
    {
        throw new NotImplementedException();
        return true;
    }

    protected override Panel panelWithControls { get { return this.PanelItemDetails; } }
}
